package com.lin.cms.domain;

import java.io.Serializable;
import java.util.Map;

import org.apache.commons.lang.math.NumberUtils;

public class Config implements Serializable {
	
	public static String LOGIN_ERROR_INTERVAL = "login_error_interval";
	
	public static String LOGIN_ERROR_TIMES = "login_error_times";

	private static final long serialVersionUID = 1L;

	private String cfgKey;

	private String cfgValue;

	private Map<String, String> attr;
	
	public static Config create(Map<String, String> map) {
		Config configLogin = new Config();
		configLogin.setAttr(map);
		return configLogin;
	}
	
	public Integer getErrorInterval() {
		String interval = getAttr().get(LOGIN_ERROR_INTERVAL);
		if (NumberUtils.isDigits(interval)) {
			return Integer.parseInt(interval);
		} else {
			// 默认间隔30分钟
			return 30;
		}
	}
	
	public Integer getErrorTimes() {
		String times = getAttr().get(LOGIN_ERROR_TIMES);
		if (NumberUtils.isDigits(times)) {
			return Integer.parseInt(times);
		} else {
			// 默认3次
			return 3;
		}
	}
	
	public Config() {
	}

	public String getCfgKey() {
		return cfgKey;
	}
	public Map<String, String> getAttr() {
		return attr;
	}

	public void setAttr(Map<String, String> attr) {
		this.attr = attr;
	}


	public void setCfgKey(String cfgKey) {
		this.cfgKey = cfgKey;
	}

	public String getCfgValue() {
		return cfgValue;
	}

	public void setCfgValue(String cfgValue) {
		this.cfgValue = cfgValue;
	}


	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((cfgKey == null) ? 0 : cfgKey.hashCode());

		return result;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final Config other = (Config) obj;
		if (cfgKey == null) {
			if (other.cfgKey != null)
				return false;
		} else if (!cfgKey.equals(other.cfgKey))
			return false;

		return true;
	}
	
	@Override
	public String toString() {
		return getClass().getName() + "@" + Integer.toHexString(hashCode()) + 
			"(" + 
			"cfgKey=" + "'" + cfgKey + "'" + 
			")";
	}
	
}
